<extend name="public/form"/>
<block name="css">
    <style>
        #content {
            width: 100%;
        }
    </style>
<!--    <script type="text/javascript" charset="utf-8" src="/static/vendor/neditor/neditor.config.js"></script>-->
<!--    <script type="text/javascript" charset="utf-8" src="/static/vendor/neditor/neditor.all.min.js"> </script>-->
<!--    <script type="text/javascript" charset="utf-8" src="/static/vendor/neditor/neditor.service.js"></script>-->
<!--    &lt;!&ndash;建议手动加在语言，避免在ie下有时因为加载语言失败导致编辑器加载失败&ndash;&gt;-->
<!--    &lt;!&ndash;这里加载的语言文件会覆盖你在配置项目里添加的语言类型，比如你在配置项目里配置的是英文，这里加载的中文，那最后就是中文&ndash;&gt;-->


    <script type="text/javascript" charset="utf-8" src="/static/vendor/ueditor1_4_3/ueditor.config.js"></script>
    <script type="text/javascript" charset="utf-8" src="/static/vendor/ueditor1_4_3/ueditor.all.min.js"> </script>

</block>
<block name="content">
    <div class="layui-card-header ">
        <span class="layui-breadcrumb">
          <a href="/admin" target="_blank">首页</a>
          <a href="javascript:;">资讯管理</a>
          <a href="javascript:;" onclick="parent.xadmin.add_tab('资讯列表','{:url('index')}')">资讯列表</a>
          <a><cite>添加资讯</cite></a>
        </span>
        <a class="layui-btn layui-btn-sm" style="line-height:1.6em;margin-top:3px;float:right"
           href="javascript:location.replace(location.href);" title="刷新">
            <i class="layui-icon layui-icon-refresh-3" style="line-height:30px"></i></a>
    </div>
    <div class="layui-card-body layui-tab">
        <form id="form1" class="layui-form">

            <div class="layui-form-item">
                <label class="layui-form-label">选择分类</label>
                <div class="layui-input-inline" style="width: auto">
                    <select name="news_category_id" id="pid-select" lay-verify="required" lay-filter="pid-select" lay-reqtext="请选择资讯分类！">
                        <option value='' data-level="1">请选择分类</option>
                        <volist name="$treeList" id="vo" empty="暂时没有数据">
                            <option value="{$vo.id}" data-level="{$vo.level}"
                            <eq name="vo.level" value="3">disabled</eq>
                            >{$vo._title}</option>
                        </volist>
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">资讯标题</label>
                <div class="layui-input-block">
                    <input type="text" name="title" lay-verify="required" lay-reqtext="请填写资讯标题！"
                           placeholder="请输入资讯标题" autocomplete="off" class="layui-input ">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">封面图</label>
                <div class="layui-input-inline" style="width: 600px">
                    <input type="text" class="layui-input "  id="cover_input" name="cover" lay-verify="required"  lay-reqtext="请上传图片">
                </div>
                <div class="layui-input-inline" style="width: 250px">
                    <b class="layui-btn" id="test1" >上传封面图</b>
                    <button class="layui-btn layui-btn-normal" id="pick_pic">选择在线图片</button>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">预览</label>
                <div class="layui-input-block">
                    <div class="layui-upload">
                        <div class="layui-upload-list">
                            <img class="layui-upload-img" id="demo1" style="max-height: 150px">
                            <p id="demoText"></p>
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">浏览量</label>
                <div class="layui-input-block">
                    <input type="text" name="browse"
                           placeholder="请输入默认浏览量" autocomplete="off" class="layui-input " value="0">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">标签</label>
                <div class="layui-input-block">
                    <div id="tags">
                        <input type="text" id="inputTags" placeholder="回车生成标签" autocomplete="off">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">操作</label>
                <div class="layui-input-block">
                    <input type="checkbox" name="top" value="1" title="置顶">
                    <input type="checkbox" name="hot" value="1" title="热门">
                    <input type="checkbox" name="recommend" value="1" title="推荐">
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">摘要</label>
                <div class="layui-input-block">
                    <textarea name="abstract" style="width: 100%;height: 90px"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">资讯内容</label>
                <div class="layui-input-block">
                    <textarea style="height:500px;" id="editor" name="content"></textarea>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="add">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</block>
<block name="js">
    <script>
        function setUrl(val){
            $('#cover_input').val(val);$('#demo1').attr('src',val);
        }
        layui.extend({
            inputTags: '/xadmin/lib/layui/lay/modules/tags/inputTags'
        }).use(['jquery', 'form', 'layer', 'upload', 'inputTags'], function () {
            var $ = layui.jquery,
                form = layui.form,
                layer = layui.layer,
                inputTags=layui.inputTags,
                upload = layui.upload;
            //创建标签
            let tags=inputTags.render({
                elem:'#inputTags',
                content: [],
                aldaBtn: false,
                done: function(value){
                    console.log(value);//啥破组件，不给获取所有数据的接口
                    tags=value;
                }
            });
            //普通图片上传
            var uploadInst = upload.render({
                elem: '#test1'
                , url: '/admin/uploads/upload?type=news_cover'
                , before: function (obj) {
                    //预读本地文件示例，不支持ie8
                    obj.preview(function (index, file, result) {
                        $('#demo1').attr('src', result); //图片链接（base64）
                    });
                }
                , done: function (res) {
                    //如果上传失败
                    if (res.code > 0) {
                        return layer.msg(res.msg);
                    }
                    layer.msg('上传成功！');
                    $("input[name='cover']").val(res.url)
                    //上传成功
                }
                , error: function () {
                    //演示失败状态，并实现重传
                    var demoText = $('#demoText');
                    demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                    demoText.find('.demo-reload').on('click', function () {
                        uploadInst.upload();
                    });
                }
            });
            //监听提交
            form.on('submit(add)', function (data) {
                //获取标签
                let send_json = data.field;
                let ems=$('#tags em');
                let tags=[];
                ems.each(function () {
                    tags.push({name:this.innerText});
                });
                //删除空的file
                delete send_json.file;
                send_json.tags=tags;
                $.ajax({
                    url: '/admin/news/add',
                    method: 'post',
                    data: send_json,
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.code == 0) {
                            layer.alert("增加成功", {
                                icon: 6
                            }, function () {
                                location.reload();
                            });
                        } else {
                            layer.msg(res.msg, {icon: 5});
                        }

                    },
                    error: function (data) {
                        layer.msg('服务器繁忙', {icon: 5});
                    }
                });
                return false;
            });
            $('#pick_pic').click(function () {
                var w=($(window).width()*0.9);
                var h=($(window).height() - 50);
                var index = layer.open({
                    type: 2,
                    area: [w+'px', h +'px'],
                    fix: false, //不固定
                    maxmin: true,
                    shadeClose: true,
                    shade:0.4,
                    title: '选择图片',
                    content: '/admin/qiniuPic/index',
                    success:function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var iframeWin = window[layero.find('iframe')[0]['name']];
                        var js_str='$(\'#cover_input\').val("{0}");$(\'#demo1\').attr(\'src\',"{0}");';
                        iframeWin.js_str=js_str;    //看这
                    }
                });
                return false
            });
            //遍历select option
            $(document).ready(function () {
                var ue = UE.getEditor('editor');
            });
        });
    </script>
</block>